package com.spring.web.adminmanage.service.impl;

import com.spring.web.adminmanage.service.CommonsWeatherService;
import com.spring.web.adminmanage.utils.CommonsWeatherUtils;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

@Service
public class CommonsWeatherServiceImpl implements CommonsWeatherService {

	@Override
	public Map<String, String> getResultMap(String city) throws Exception {
		// 去掉后缀
		if (city.indexOf("市") > 0) {
			city = city.replace("市", "");
		} else if (city.indexOf("地区") > 0) {
			city = city.replace("地区", "");
		}  else if (city.indexOf("县") > 0) {
			city = city.replace("县", "");
		}else if (city.indexOf("族") > 0) {
			city = city.substring(0, city.indexOf("族") - 1);
		}
		String link = "http://php.weather.sina.com.cn/xml.php?city=" + URLEncoder.encode(city, "GBK")
				+ "&password=DJOYnieT8234jlsK&day=0";
		URL url = new URL(link);
		CommonsWeatherUtils parser = new CommonsWeatherUtils(url);
		String[] nodes = { "city", "status1", "temperature1", "status2", "temperature2" };
		Map<String, String> resultMap = parser.getValue(nodes);
		return resultMap;
	}


}
